//
//  DHStatusViewModel.swift
//  Weibo
//
//  Created by Holfioo on 15/9/7.
//  Copyright © 2015年 Holfioo. All rights reserved.
//

import UIKit

let DHStatusCellID = "StatusCellID"
let DHStatusRetweetID = "StatusRetweetID"

class DHStatusViewModel: NSObject {
    // 视图模型中存放status
    var status: DHStatus
    // 设置变量保存行高
    var rowHeight: CGFloat = 0
    // 辨别是 转发的 还是 原创的
    var cellID: String {
        return status.retweeted_status?.pic_urls == nil ? DHStatusCellID : DHStatusRetweetID
    }
    
    // 转发内容的样式
    var retweetText: String? {
        let username = status.retweeted_status?.user?.name ?? ""
        let text = status.retweeted_status?.text ?? "" // 如果没有返回空
        return "@\(username):\(text)"
    }
    
    // 计算型属性
    var userIconUrl: NSURL? {
        return NSURL(string: status.user?.profile_image_url ?? "")
    }
    // 设置会员图标
    var userVipImage: UIImage? {
        switch (status.user?.verified ?? -1) {
        case 0: return UIImage(named: "avatar_vip")
        case 2, 3, 5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
            // 注意。如果是什么也没有。注意返回default
        default: return nil
        }
    }
    
    var userMember: UIImage? {
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            // 如果能到这里，代表一定有值。
            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        return nil
    }
    // 存放的都是url
    var thumbnailUrls: [NSURL]?
    
    // 计算型属性来保存middle图片
    var bmiddleUrls: [NSURL]? {
        // 确保有值
        guard let urls = thumbnailUrls else {
            return nil
        }
        // 遍历
        var bmiddleUrls = [NSURL]()
        // 遍历
        for str in urls {
            // 通过替换/.../ 确保不会出现重复字符段
            let urlStr = str.absoluteString.stringByReplacingOccurrencesOfString("/thumbnail/", withString: "/bmiddle/")
            bmiddleUrls.append(NSURL(string: urlStr)!)
        }
        return bmiddleUrls
    }
    
    // 构造方法
    // 将值传入到模型中
    init(status: DHStatus) {
        
        self.status = status
        // 进行重构
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls {
            thumbnailUrls = [NSURL]()
            // 遍历路径的字典
            for dict in urls {
                // 由于是个字典数组
                thumbnailUrls?.append(NSURL(string: dict["thumbnail_pic"]!)!)
            }
        }
        
        /*
        // 注意数组初始化
        thumbailUrls = [NSURL]()
        if status.pic_urls != nil {
            for dict in status.pic_urls! {
                // 由于是个字典数组
                thumbailUrls?.append(NSURL(string: dict["thumbnail_pic"]!)!)
            }
        }
        // 转发微博的数组不为空
        if status.retweeted_status?.pic_urls != nil {
            thumbailUrls = [NSURL]()
            for dict in status.retweeted_status!.pic_urls! {
                thumbailUrls?.append(NSURL(string: dict["thumbnail_pic"]!)!)
            }
        } */
        
        super.init()
    }
}
